package com.example.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.Mapper.RoleMapper;
import com.example.demo.dto.RoleDto;
import com.example.demo.entity.Role;
import com.example.demo.service.RoleService;
import com.example.demo.vo.RoleMenuParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    private RoleMapper roleMapper;

    public RoleServiceImpl(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    @Override
    public List<RoleDto> selectRole() {
        final List<Role> roles = roleMapper.selectList(null);
        List<RoleDto> list = new ArrayList<>();
        for(int i = 0; i < roles.size(); i++) {
            final Role item = roles.get(i);
            RoleDto roleDto = new RoleDto();
            roleDto.setRoleName(item.getRoleName());
            roleDto.setExplainText(item.getExplainText());
            roleDto.setCreateTime(item.getCreateTime());
            roleDto.setId(item.getId());
            roleDto.setMenuId(item.getMenuId().split(","));
            roleDto.setPromisitionId(item.getPromisitionId().split(","));
            list.add(roleDto);
        }
        return list;
    }

    @Override
    public int deleteRoleById(String id) {
        final int i = roleMapper.deleteById(id);
        return i;
    }

    @Override
    public int insertRole(RoleDto roleDto) throws ParseException {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());

        final Role role = roleMapper.selectById(roleDto.getId());
        role.setId(roleDto.getId());
        role.setUpdateTime(formatter.parse(formatter.format(date)));
        role.setExplainText(roleDto.getExplainText());
        role.setRoleName(roleDto.getRoleName());
        role.setMenuId(StringUtils.join(roleDto.getMenuId(), ","));
        role.setPromisitionId(StringUtils.join(roleDto.getPromisitionId(), ","));
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", roleDto.getId());
        final int update = roleMapper.update(role, queryWrapper);
        return  update;
    }

    @Override
    public List<RoleDto> likeRole(String key) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("role_name", key);
        final List<Role> roles = roleMapper.selectList(queryWrapper);

        List<RoleDto> list = new ArrayList<>();

        for(int i = 0; i < roles.size(); i++) {
            final Role item = roles.get(i);
            RoleDto roleDto = new RoleDto();
            roleDto.setRoleName(item.getRoleName());
            roleDto.setExplainText(item.getExplainText());
            roleDto.setCreateTime(item.getCreateTime());
            roleDto.setId(item.getId());
            roleDto.setMenuId(item.getMenuId().split(","));
            roleDto.setPromisitionId(item.getPromisitionId().split(","));
            list.add(roleDto);
        }
        return list;
    }

    @Override
    public int updateRoleMenu(RoleMenuParams roleMenuParams) {

        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", roleMenuParams.getId());

        final Role role = roleMapper.selectOne(queryWrapper);

        role.setMenuId(StringUtils.join(roleMenuParams.getChild(), ","));
        final int update = roleMapper.update(role, queryWrapper);
        return update;
    }

    @Override
    public int updateRoleInfo(Role role) {

        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", role.getId());
        final Role role1 = roleMapper.selectOne(queryWrapper);

        role1.setRoleName(role.getRoleName());
        role1.setExplainText(role.getExplainText());
        System.out.println(444);
        System.out.println(role1);

        final int update = roleMapper.update(role1, queryWrapper);
        return update;
    }

    @Override
    public int addRole(Role role) {
        role.setMenuId("999");
        role.setPromisitionId("999");
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        final int insert = roleMapper.insert(role);
        return insert;
    }
}
